from django.views import View

# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection
# 获取分页
from SaaSDevelTool.data_operation import paging_tool_join

"页面获取数据"


class SaaSDataSumResourceQuery(View):
    @error_message_detection
    def get(self, request):
        # 获取页面页码
        page_number = request.GET.get("page_number")
        # 获取每页尺寸
        page_size = request.GET.get("page_size")
        # 获取名称
        resource_name = request.GET.get("resource_name")
        # 判断获取出来的参数
        if not all([page_size, page_number]):
            return create_response(code=400, msg="必传参数未传递完整。")

        # 判断名称是否被传递
        if resource_name:
            where_resource_mame_sql = """ AND s.resource_name LIKE '%{}%'""".format(resource_name)
        else:
            where_resource_mame_sql = """"""

        # 拼接sql语句
        query_sum_page_resource_table_sql = """
            SELECT 
                s.resource_id,
                s.resource_name,
                s.resource_remark,
                s.resource_urls,
                s.resource_home_page,
                s.resource_images,
                spt.package_name 
            FROM 
                db_saas_center.sum_page_resource_table s
            LEFT JOIN  
                db_saas_center.sum_package_table spt 
            ON  
                s.package_id = spt.package_id 
            WHERE 
                1=1 {}
            ;""".format(where_resource_mame_sql)
        # 获取结果
        query_sum_page_resource_table_data = paging_tool_join(
            query_sum_page_resource_table_sql, int(page_number), int(page_size)
        )
        # 返回结果
        return create_response(code=200, msg="", data=query_sum_page_resource_table_data)
